package com.ruoyi.system.quartz;


import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.LabinfoItemData;
import com.ruoyi.system.mapper.LabinfoItemDataMapper;
import com.ruoyi.system.mapper.SysMessageMapper;
import com.ruoyi.system.mapper.tumor.BussinessDataMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Component("reportingDateTask")
@EnableScheduling
public class ReportingDateTask {

    private static final Logger log = LoggerFactory.getLogger(ReportingDateTask.class);


    @Autowired
    private BussinessDataMapper bussinessDataMapper;

    @Autowired
    private LabinfoItemDataMapper labinfoItemDataMapper;


//    @Scheduled(cron = "0 0/2 * * * ? ") //每隔2分钟执行异常
    @Scheduled(cron = "0 0 9,23 * * ? ") //每天9点和23点执行一次
    private void getReportingDate() throws ParseException {
        //先查出 item表中，所有的 未删除的数据
        List<LabinfoItemData> labinfoItemDataList = labinfoItemDataMapper.selectLabinfoItemDataListByReportingDateisNull();
        SimpleDateFormat limsFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
        if(CollectionUtil.isNotEmpty(labinfoItemDataList)){
            try {
                for(LabinfoItemData labinfoItemData :labinfoItemDataList){
                    //送检单编号
                    String sampleSerialNumber = labinfoItemData.getSampleSerialNumber();
                    if(StringUtils.isNotEmpty(sampleSerialNumber)){
                        Map map = bussinessDataMapper.getDeliverDate(sampleSerialNumber);
                        if(map != null){ //修改bull
                            if(map.get("DeliverDate") !=null){
                                String deliverDate = map.get("DeliverDate").toString();
                                if(StringUtils.isNotEmpty(deliverDate)){
                                    Date date = limsFormat.parse(deliverDate);
                                    labinfoItemData.setReportingDate(date);
                                    labinfoItemDataMapper.updateLabinfoItemData(labinfoItemData);
                                }
                            }
                        }
                    }


                }
            }catch (Exception e){
                log.error(e.getMessage());
            }
        }
    }
}
